package indi.zhifa.study2025.test.kafka.producer.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.support.config.FastJsonConfig;
import indi.zhifa.study2025.test.kafka.producer.entity.KafkaData;
import indi.zhifa.study2025.test.kafka.producer.service.IKafkaSendData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class KafkaSendDataImpl implements IKafkaSendData {

    private final KafkaTemplate<String, String> mKafkaTemplate;
    private final FastJsonConfig mFastJsonConfig;

    public KafkaSendDataImpl(KafkaTemplate<String, String> pKafkaTemplate,
                             @Qualifier("simple-fastJson-config") FastJsonConfig pFastJsonConfig) {
        mKafkaTemplate = pKafkaTemplate;
        mFastJsonConfig = pFastJsonConfig;
    }


    @Override
    public void sendAsync(String topic,KafkaData pKafkaData) {
        String str = JSON.toJSONString(pKafkaData);
        try{
            mKafkaTemplate.send(topic,pKafkaData.getName(),str);
        }catch (Exception e){
            log.error("发送kafka时发成错误，错误信息是"+ e.getMessage());
        }

    }
}
